package com.joe.bibackend.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class ExcelUtil {


    public static String convertExcelToCVS(MultipartFile file){
        try {
            List<Map<Integer,String>> listResult =
                    EasyExcel.read(file.getInputStream())
                            .excelType(ExcelTypeEnum.XLSX)
                            .sheet()
                            .headRowNumber(0)
                            .doReadSync();

            StringBuilder finalResult = new StringBuilder();
            for(Map<Integer,String> map : listResult){
                Collection<String> resultList = map.values();
                for(String value: resultList){
                    finalResult.append(value).append(",");
                }
                finalResult.delete(finalResult.lastIndexOf(","),finalResult.lastIndexOf(",")+1).append("/n");
            }
            return finalResult.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
